<template>
  <div>
    <div class="middle">
      <el-tabs v-model="activeName">
        <el-tab-pane label="密码修改" name="pass">
          <el-form
            :model="ruleForm"
            status-icon
            :rules="rules"
            ref="ruleForm"
            label-width="100px"
            class="demo-ruleForm"
          >
            <el-form-item label="账户" prop="name">
              <el-input v-model="ruleForm.name" disabled></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="pass">
              <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="确认密码" prop="checkPass">
              <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
            </el-form-item>

            <el-form-item>
              <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
              <el-button @click="resetForm()">重置</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>

<script>
import {
 up_adminPass
} from "@/api/user/api";
export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      adminData:JSON.parse(localStorage.getItem("admin")),
      activeName: "pass",
      ruleForm: {
        name: "",
        pass: "",
        checkPass: ""
      },
      rules: {
        pass: [{ validator: validatePass, trigger: "blur" }],
        checkPass: [{ validator: validatePass2, trigger: "blur" }]
      }
    };
  },
  created() {
    
    this.ruleForm.name = this.adminData.name
  },
  methods: {
    // 重置
    resetForm() {
      this.ruleForm.pass = "";
      this.ruleForm.checkPass = "";
    },
     submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            let query={
              id:this.adminData.id,
              pass:this.ruleForm.pass
            }
            up_adminPass(query).then(res=>{
              let data=res.data
              if (data == "ok") {
                this.resetForm();
                this.$message.success("密码修改成功！！！");
              }
              
            })
          } else {
            
            return false;
          }
        });
      },
  }
};
</script>
<style>
.meaty .avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.meaty .avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.meaty .avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 112px;
  height: 112px;
  line-height: 112px;
  text-align: center;
}
.meaty .avatar {
  width: 112px;
  height: 112px;
  display: block;
}
</style>
<style>
.el-table--border .el-table__cell {
  border: none;
}
</style>
<style scoped>
.middle {
  background: #fff;
  margin: 20px 15px;
}
.middle > div {
  padding: 20px 20px 10px 20px;
}
.search {
  margin: 10px 15px 0 15px;
}
.paging {
  display: flex;
  justify-content: end;
}
.pagination-container {
  margin-left: auto;
  margin-bottom: 20px;
}
</style>